package com.kabuda.blog.controller;

import com.kabuda.blog.dao.pojo.SysUser;
import com.kabuda.blog.service.IUserService;
import com.kabuda.blog.vo.Result;
import com.kabuda.blog.vo.SysUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kabuda
 * @create 2023-04-02 17:40
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @ApiOperation("获取列表数据")
    @GetMapping("/list")
    public Result list(){
        List<SysUserVo> list = userService.listVo();
        return Result.success(list);
    }
    @ApiOperation("修改状态（删除和非删除代替）")
    @GetMapping("/status")
    public Result changeStatus(@RequestParam("status") Integer status, @RequestParam("id") Long id){
        int count=userService.changeStatus(status,id);
        if(count==1){
            return Result.success(null);
        }else{
            return Result.fail(333,"删除失败");
        }
    }
    @ApiOperation("根据id查找用户")
    @GetMapping("{id}")
    public Result getById(@PathVariable("id") Long id){
       SysUserVo sysUserVo= userService.getUserById(id);
       return Result.success(sysUserVo);
    }
    @ApiOperation("修改用户信息")
    @PostMapping("edit")
    public Result edit(@RequestBody SysUser sysUser){
       userService.edit(sysUser);
       return Result.success(null);
    }
    @ApiOperation("新增用户信息")
    @PostMapping("add")
    public Result add(@RequestBody SysUser sysUser){
       userService.addUser(sysUser);
       return Result.success(null);
    }
    @ApiOperation("删除用户信息")
    @GetMapping("delete/{id}")
    public Result add(@PathVariable("id") Long id){
        userService.deleteUser(id);
       return Result.success(null);
    }
}
